<?php
/**
 * @package Akimimi
 * @version $Id$
 */

/**
 * ebiz front
 *
 * pager
 *
 * @author allen
 * @desc unfriendly re-write this class, let it allow FORM
 */
class PagerNew{

	public $curNumStyle = '';
	public $numLinkStyle = '';
	public $spaceElement = '';
	public $pagerCssFront = '';
	public $pagerCssBack = '';
	
	protected $countPage = 0;
	protected $currentPage = 1;
    protected $totalRows = 0;
    protected $route = '';
    protected $displayRows = 1;
    protected $submitByPost = false;
    protected $formId = '';
    protected $pageId = '';
    protected $pageListLength = 9;
    protected $defaultCurrentPagePlace;
    protected $pagelimit = '';
    protected $callJavascript = false;

    public function setJavascriptFunction( $str_functionname ){
        $this->callJavascript = $str_functionname;
    }

    /**
     * Get default set of page size
     *
     * @return int
     */
    public static function GetDefaultPagesize(){
        return $GLOBALS['config']['items_per_page'];
    }

    /**
     * Note: $int_nowpage use for show on HTML, start point is 1
     *
     * @param int $int_nowpage
     * @param int $int_pagesize
     * @return int
     */
    public static function GetPageLimitStartNumber($int_nowpage, $int_pagesize = null){
        if( empty($int_pagesize) )
            $int_pagesize = Pager::GetDefaultPagesize();
        if( $int_nowpage < 1 )
            $int_nowpage = 1;
        return ( $int_nowpage - 1 ) * $int_pagesize;
    }

    /**
     * Get max page number
     *
     * @param int $int_count this value usually from select count(*) from `table`
     * @param int $int_pagesize
     * @return int
     */
    public static function GetCountPage($int_count, $int_pagesize = null){
        if( empty($int_pagesize) )
            $int_pagesize = Pager::GetDefaultPagesize();
        return ceil($int_count/$int_pagesize);
    }

    /**
     * validate now page number
     *
     * @param int $int_nowpage this param is reference
     * @param int $int_count
     * @param int $int_pagesize
     */
    public static function ValidateNowPage(&$int_nowpage, $int_count, $int_pagesize = null){
        if( $int_nowpage < 1 || empty($int_nowpage) ){
            $int_nowpage = 1;
        }else{
            $int_countpage = Pager::GetCountPage($int_count,$int_pagesize);
            if( $int_nowpage > $int_countpage )
                $int_nowpage = $int_countpage;
        }
    }

    /**
     * construct function created by M.Wang
     * only use for submit 'FORM' if it is necessary
     */
    public function __construct($str_formId=null, $str_pageId=null, $bool_post=false){
         $this->formId = $str_formId;
         $this->pageId = $str_pageId;
         $submitByPost = $bool_post;
         $this->defaultCurrentPagePlace = ceil( $this->pageListLength / 2 );
         $this->pagelimit = Pager::getDefaultPagesize();
    }

    /**
     * Original function
     * @param int $int_currentPage
     * @param int $int_totalRows
     * @param string $str_route action name
     * @param int $int_pagesize how much records will shown
     * @return string
     */
    public function paginate($int_currentPage=1, $int_totalRows, $str_route, $int_pagesize=null ){
        if( empty( $int_totalRows ) )
            return '';
        if( empty( $int_pagesize ) )
            $int_pagesize = $this->pagelimit;
        $this->currentPage = $int_currentPage;
        $this->totalRows = $int_totalRows;
        $this->route = $str_route;
        if( !preg_match( '/\/$/', $this->route ) )
            $this->route.= '/';
        $this->displayRows = $int_pagesize;
        $this->countPage = ceil($int_totalRows/$int_pagesize);
        return $this->getPageList();
    }

    protected function getClickSetting($int_jumpPage){
    	if( !$this->submitByPost && !$this->callJavascript && empty($this->formId) )
    	   $str_result = "window.location='{$this->route}{$int_jumpPage}';";
    	elseif( !$this->callJavascript )
    	   $str_result = "$('#{$this->pageId}').val({$int_jumpPage});"
    	               . "$('#{$this->formId}').submit();";
    	else
    	   $str_result = "{$this->callJavascript}({$int_jumpPage})";
    	return $str_result;
    }

    protected function getPageElement($int_jumpPage, $str_text=null){
    	if( $int_jumpPage == $this->currentPage )
    	   return "<a href='javascript:void(0);' style='{$this->curNumStyle}'>{$int_jumpPage}</a>";
    	if( !$this->submitByPost && !$this->callJavascript && empty($this->formId) )
    	   return "<a href='{$this->route}{$int_jumpPage}' style='{$this->numLinkStyle}'>" . (empty($str_text)?$int_jumpPage:$str_text) . "</a>";
    	return "<a href='javascript:void(0);' style='{$this->numLinkStyle}' onclick=\""
    	       . $this->getClickSetting($int_jumpPage)
    	       . "\" >"
    	       . (empty($str_text)?$int_jumpPage:$str_text)
    	       . "</a>";
    }

    protected function getSpaceMark(){
    	return $this->spaceElement;
    }

    protected function getPageList(){
    	$bool_startSpace = false;
    	$bool_endSpace = false;

    	if( $this->countPage <= $this->pageListLength){
    	    $int_startNumber = 1;
    	    $int_endNumber = $this->countPage;
    	}else{

            if( $this->currentPage - $this->defaultCurrentPagePlace > 0 ){
                $bool_startSpace = true;
                $int_startNumber = $this->currentPage - 2;
            }else{
                $int_startNumber = 1;
            }
            if( $this->currentPage + $this->defaultCurrentPagePlace - 1 < $this->countPage ){
                $bool_endSpace = true;
                $int_endNumber = $this->currentPage + 2;
            }else{
                $int_endNumber = $this->countPage;
            }
            if( $bool_endSpace != $bool_startSpace ){
                if( $bool_endSpace )
                    $this->pageListLength -= 2;
                if( $bool_startSpace )
                    $this->pageListLength -= 2;
                if( $int_endNumber - $int_startNumber + 1 < $this->pageListLength ){
                    if( $bool_endSpace )
                        $int_endNumber = $int_startNumber + $this->pageListLength - 1;
                    if( $bool_startSpace )
                        $int_startNumber = $int_endNumber - $this->pageListLength + 1;
                }
            }

            if( $int_startNumber < 1 )
                $int_startNumber = 1;
            if( $int_endNumber > $this->countPage )
                $int_endNumber = $this->countPage;

    	}

        $str_result = ($bool_startSpace ? $this->getPageElement(1) . $this->getSpaceMark() : '');
        $str_result.= $this->getMultiPageElements($int_startNumber, $int_endNumber);
        $str_result.= ($bool_endSpace ? $this->getSpaceMark() . $this->getPageElement($this->countPage) : '');
        return $this->pagerCssFront.$str_result.$this->pagerCssBack;;
    }

    protected function getMultiPageElements($int_start, $int_end){
    	if( $int_end < $int_start )
    	   return '';
    	$str_result = '';
    	for($int_i=$int_start; $int_i <= $int_end; $int_i ++ )
    	   $str_result.= $this->getPageElement($int_i);
    	return $str_result;
    }

    public function _paginate($cpage = 1, $totalCount, $route, $pagesize = 2){   //example: $route=/c/xx/
        $internationalizationPath = "../$GLOBALS[local]/"."common.php";
        require_once $internationalizationPath;
    	$path = $route;
    	$cp = $cpage; //current page
        $count = $totalCount; //total number of product number
        $tp = $count / $pagesize; //show total number of category page
        $offset = 2; //set the offset number
        if(!is_int($tp)){
            $tp = intval($tp) + 1;
        }
        $pageHtml = "";
        if($tp <= 7){
            if($cp >= $tp){
                $cp = $tp;
            }
            if($cp < 1){
                $cp = 1;
            }
            if($cp == 1){
                $pageHtml .= "<span class='paginateDisabled'>&laquo; ".PREV." </span>";
            }else{
                $pageHtml .= "<span class='paginateNormal'><a  href='$path".intval($cp - 1)."' >&laquo; ".PREV."</a></span>";
            }
            for($i = 1; $i <= $tp; $i++){
                if($i == $cp){
                    $pageHtml .= "<span class='paginateActived'>".$i."</span>";
                }else{
                    $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$i."' >".$i."</a></span>";
                }
            }
            if($cp >= $tp){
                $pageHtml .= "<span class='paginateDisabled'>".NEXT." &raquo;</span>";
            }else{
                $pageHtml .= "<span class='paginateNormal'><a href='".$path.intval($cp + 1) ."' > ".NEXT." &raquo;</a></span>";
            }
        }else{
        	if($cp <= 2 + $offset){//if tp=8//show 1 2 3 4 5 6 ... tp
	            if($cp < 1){
	                $cp = 1;
	            }
	            if($cp == 1){
	                $pageHtml .= "<span class='paginateDisabled'>&laquo; ".PREV." </span>";
	                $pageHtml .= "<span class='paginateActived'> 1 </span>";
	            }else{
	                $pageHtml .= "<span class='paginateNormal'><a  href='$path".intval($cp - 1)."' >&laquo; ".PREV."</a></span>";
	                $pageHtml .= "<span class='paginateNormal'><a  href='$path"."1"."' > 1 </a></span>";
	            }
	            for($i = 2; $i <= $cp + $offset; $i++){
	                if($i == $cp){
	                    $pageHtml .= "<span class='paginateActived'>".$i."</span>";
	                }else{
	                    $pageHtml .= "<span class='paginateNormal'><a href='".$path.$i."' >".$i."</a></span>";
	                }
	            }
	            $pageHtml .= "<span class='paginateSeperator'> ... </span>";
	            $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$tp ."' >$tp</a></span>";
	            $pageHtml .= "<span class='paginateNormal'><a  href='".$path.intval($cp + 1) ."' > ".NEXT." &raquo;</a></span>";
        	}elseif($cp >= $tp - 1 - $offset){//if tp=8//show 1 ... 3 4 5 6 7 tp
                if($cp >= $tp){
                    $cp = $tp;
                }
                $pageHtml .= "<span class='paginateNormal'><a href='$path".intval($cp - 1)."' >&laquo; ".PREV."</a></span>";
                $pageHtml .= "<span class='paginateNormal'><a  href='$path"."1"."' > 1 </a></span>";
                $pageHtml .= "<span class='paginateSeperator'> ... </span>";
                for($i = $cp - $offset; $i < $tp; $i++){
                    if($i == $cp){
                        $pageHtml .= "<span class='paginateActived'>".$i."</span>";
                    }else{
                        $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$i."' >".$i."</a></span>";
                    }
                }
                if($cp >= $tp){
                    $pageHtml .= "<span class='paginateActived'> $tp </span>";
                    $pageHtml .= "<span class='paginateDisabled'> ".NEXT." &raquo;</span>";
                }else{
                    $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$tp ."' >$tp</a></span>";
                    $pageHtml .= "<span class='paginateNormal'><a  href='".$path.intval($cp + 1) ."' > ".NEXT." &raquo;</a></span>";
                }
        	}else{//if tp=10 cp=5//show 1 ... 3 4 5 6 7 ... tp
                $pageHtml .= "<span class='paginateNormal'><a  href='$path".intval($cp - 1)."' >&laquo; ".PREV."</a></span>";
                $pageHtml .= "<span class='paginateNormal'><a  href='$path"."1"."' > 1 </a></span>";
                $pageHtml .= "<span class='paginateSeperator'> ... </span>";
                for($i = $cp - $offset; $i <= $cp + $offset; $i++){
                    if($i == $cp){
                        $pageHtml .= "<span class='paginateActived'>".$i."</span>";
                    }else{
                        $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$i."' >".$i."</a></span>";
                    }
                }
                $pageHtml .= "<span class='paginateSeperator'> ... </span>";
                $pageHtml .= "<span class='paginateNormal'><a  href='".$path.$tp."' >$tp</a></span>";
                $pageHtml .= "<span class='paginateNormal'><a  href='".$path.intval($cp + 1) ."' > ".NEXT." &raquo;</a></span>";
        	}
        }
        return $pageHtml;
    }


}









